<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '230px'
    }
  },
  data() {
    return {
      chart: null,
      color:''
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  watch:{
    theme(n) {
    return n
    }
  },
  computed:{
    theme() {
      console.log(this.$store.state.settings.theme);
      return this.$store.state.settings.theme?'#fff':'#333';
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons');
      var total=459;
      var color=this.theme;
      this.chart.setOption({
        title: {
          zlevel: 0,
          text: [
              '{value|' + total + '}',
              '{name|总数}',
          ].join('\n'),
          top: 'center',
          left: 'center',
          textAlign: 'center',
          textStyle: {
              rich: {
                  value: {
                      color:color,
                      fontSize: 30,
                      fontWeight: 'bold',
                      lineHeight: 40,
                  },
                  name: {
                      color: '#909399',
                      fontSize: 14,
                      lineHeight: 20
                  },
              },
          },
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          right: 10,
          orient: 'vertical',
          icon: "circle",
          itemGap:20,
          itemWidth:8,
          itemHeight:8,
          textStyle:{borderRadius:10},
          data: ['结算', '送餐', '扫房', '发票', '报修', '报警','其他']
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            // roseType: 'radius',
            radius: [50, 70],
            center: ['47%', '50%'],
            label: {
                show: true,
                // position: 'center'
            },
            labelLine: {
                show: true
            },
            emphasis: {
                label: {
                    show: true,
                    fontSize: '24',
                    fontWeight: 'bold'
                }
            },
            data: [
              { value: 36, name: '结算' },
              { value: 10, name: '送餐' },
              { value: 10, name: '扫房' },
              { value: 10, name: '发票' },
              { value: 10, name: '报修' },
              { value: 10, name: '报警' },
              { value: 14, name: '其他' }
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
